home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / GL / demograph / colors.c next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.2 KB  |  181 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include "gl.h"
  18. #include "stdio.h"
  19. #include "demograph.h"
  20.  
  21. /* color cube for fast indexing of rgb values*/
  22. static Colorindex colorcube[512];
  23.  
  24. /* set TRUE when colorcube initialized */
  25. static int cminited;
  26. static int inRGB = FALSE;
  27.  
  28. /* functions in this file */
  29. void init_cmRGB(void);
  30. Colorindex to16(short rgb[3], short palette[16][3]);
  31. void make16map(short palette[16][3]);
  32. void setcolor(int r, int g, int b);
  33.  
  34.  
  35. /* ************************************************************************
  36.    Init_cmRGB must be called before cmRGB is used.  This function builds an
  37.    8x8x8x2 r g b color cube.  There are 512 entries of pairs of indices from
  38.    the lower 16 bits of the SGI 3.2 color map that, when dithered, match as
  39.    close as possible the r g b values of the color cube.
  40.    ************************************************************************ */
  41. void init_cmRGB(void)
  42. {
  43.     register short  r, g, b;
  44.     register Colorindex *mapptr;
  45.     short           c[3];
  46.     short           palette[16][3];
  47.  
  48.     cminited = TRUE;
  49.  
  50.     if (getgdesc(GD_BITS_NORM_DBL_RED) == 0 || 
  51.         getgdesc(GD_BITS_NORM_DBL_BLUE) == 0 ||
  52.         getgdesc(GD_BITS_NORM_DBL_GREEN) == 0) {
  53.     cmode();
  54.     gconfig();
  55.     } else {
  56.     RGBmode();
  57.     gconfig();
  58.     inRGB = TRUE;
  59.     return;
  60.     }
  61.  
  62.     /* make array of lower 16 color values */
  63.     make16map(palette);
  64.  
  65.     /* fill in color cube */
  66.     mapptr = &colorcube[0];
  67.     for (b = 0; b < 8; b++) {
  68.     for (g = 0; g < 8; g++) {
  69.         for (r = 0; r < 8; r++) {
  70.         /* 3 bits each = 7R,7G,7B - scale 255 values down to 7 */
  71.         c[0] = r * 255.0 / 7.0;
  72.         c[1] = g * 255.0 / 7.0;
  73.         c[2] = b * 255.0 / 7.0;
  74.         *mapptr++ = to16(c, palette);
  75.         }
  76.     }
  77.     }
  78. }
  79.  
  80.  
  81. /* ************************************************************************
  82.    To16 recieves an rgb array and returns the closest values from the bottom 16
  83.    colors of the SGI colormap (palette).
  84.    ************************************************************************ */
  85. Colorindex to16(rgb,palette)
  86. short rgb[];
  87. short palette[][3];
  88. {
  89.     register int    i, dr, dg, db, bestdist, s, bestmatch;
  90.  
  91.     bestdist = 1 << 30;
  92.     for (i = 0; i < 16; i++) {
  93.     /* if color matches return color without calculating distance */
  94.     if (palette[i][0] == rgb[0] &&
  95.         palette[i][1] == rgb[1] &&
  96.         palette[i][2] == rgb[2]) {
  97.         return ((Colorindex) i);
  98.     }
  99.     /* get distance to color */
  100.     dr = palette[i][0] - rgb[0];
  101.     dg = palette[i][1] - rgb[1];
  102.     db = palette[i][2] - rgb[2];
  103.     s = (dr * dr + dg * dg + db * db);
  104.  
  105.     /* pick the shortest distance */
  106.     if (s < bestdist) {
  107.         bestmatch = i;
  108.         bestdist = s;
  109.     }
  110.     }
  111.     return ((Colorindex) bestmatch);
  112. }
  113.  
  114.  
  115. /* *************************************************************************
  116.    This routine builds a palette array for the rgb values of the lower 16 bits
  117.    of the color map.  It is hard wired because the default colors for this part
  118.    of the map are supposedly fixed.  I didn't read colors from the color map as
  119.    some application may have altered these
  120.    ************************************************************************* */
  121. void make16map(palette)
  122. short palette[][3];
  123. {
  124.     short           i;
  125.  
  126.     for (i = 0; i < 8; i++) {
  127.     palette[i][0] = (i & 0x0001) * 255;
  128.     palette[i][1] = ((i & 0x0002) >> 1) * 255;
  129.     palette[i][2] = ((i & 0x0004) >> 2) * 255;
  130.     }
  131.  
  132.     palette[8][0] = 85;
  133.     palette[8][1] = 85;
  134.     palette[8][2] = 85;
  135.     palette[9][0] = 198;
  136.     palette[9][1] = 113;
  137.     palette[9][2] = 113;
  138.     palette[10][0] = 113;
  139.     palette[10][1] = 198;
  140.     palette[10][2] = 113;
  141.     palette[11][0] = 142;
  142.     palette[11][1] = 142;
  143.     palette[11][2] = 56;
  144.     palette[12][0] = 113;
  145.     palette[12][1] = 113;
  146.     palette[12][2] = 198;
  147.     palette[13][0] = 142;
  148.     palette[13][1] = 56;
  149.     palette[13][2] = 142;
  150.     palette[14][0] = 56;
  151.     palette[14][1] = 142;
  152.     palette[14][2] = 142;
  153.     palette[15][0] = 170;
  154.     palette[15][1] = 170;
  155.     palette[15][2] = 170;
  156. }
  157.  
  158.  
  159. /* ************************************************************************
  160.    This is the routine all the setup was dome for.  This routine sets an RGB
  161.    color if the machine is in RGB mode.  It sets a colormap color if the
  162.    machine is in color map mode
  163.    ************************************************************************ */
  164. void setcolor(r,g,b)
  165. int r,g,b;
  166. {
  167.     /* set color mode and initialize palette if necessary */
  168.     if (!cminited) {
  169.     init_cmRGB();
  170.     }
  171.  
  172.     if (inRGB) {
  173.     /* set RGBcolor if in RGB mode */
  174.     RGBcolor(r, g, b);
  175.     } else {
  176.     /* set colormap color if in colormap mode */
  177.     color(colorcube[((r >> 5) + ((g & 0xe0) >> 2) + ((b & 0xe0) << 1))]);
  178.     }
  179. }
  180.  
  181.